package com.naiterui.ehp.ps.sms.factory;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.ps.sms.enums.MontnetStatus;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.vo.MessageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 梦网短信平台
 *
 * @author
 * @since 1.0.0
 */
@Slf4j
public class MontnetSms {

    /**
     * 单条发送短信
     * MontnetSms.siggleSend()
     *
     * @param phone
     * @param content
     *
     * @return
     *
     * @throws SmsBusinessException
     * @Author
     * @Date 2017年7月26日
     * @since 1.0.0
     */
    public static MessageResponseVO siggleSend(String phone, String businessId, String content) throws SmsBusinessException {
        String result = MontnetApi.siggleSend(phone, businessId, content);
        log.info("[发送短信，梦网平台]--短信发送完成，手机号电话号码:[{}] 业务ID:{} 发送内容:[{}] 第三方平台返回结果:[{}]", phone, businessId, content, result);
        MessageResponseVO responseVO = processApiResult(result);
        return responseVO;
    }

    /**
     * 处理API返回的结果
     * MontnetSms.processApiResult()
     *
     * @param result
     *
     * @return
     *
     * @throws SmsBusinessException
     * @Author
     * @Date 2017年7月26日
     * @since 1.0.0
     */
    private static MessageResponseVO processApiResult(String result) throws SmsBusinessException {
        // 处理api请求结果
        if (StringUtils.isEmpty(result)) {
            log.error("梦网平台返回结果为空：[{}]", result);
            throw new SmsBusinessException(ExceptionCodes.SMS_PLATFORM_RETURN_NULL);
        }
        JSONObject resultJson = JSONObject.parseObject(result);
        int statusCode = resultJson.getInteger("result");
        MessageResponseVO responseVO = new MessageResponseVO();
        if (MontnetStatus.SUCCESS.code == statusCode) {
            responseVO.setStatusCode(String.valueOf(ExceptionCodes.SUCCESS));
        } else {
            Integer covertCode = convertErrorCode(statusCode);
            if (covertCode != null) {
                log.error("梦网平台返回状态码转换错误,statusCode：{}", statusCode);
                throw new SmsBusinessException(covertCode);
            } else {
                responseVO.setStatusCode(resultJson.getString("result"));
            }
        }
        responseVO.setStatusMsg("");
        return responseVO;
    }

    /**
     * 转换梦网返回的错误码
     * MontnetSms.convertErrorCode()
     *
     * @param statusCode
     *
     * @return
     *
     * @Author
     * @Date 2017年8月4日
     * @since 1.0.0
     */
    private static Integer convertErrorCode(Integer statusCode) {
        if (MontnetStatus.PHONE_INVALID.code == statusCode) {
            // 非法的手机号码
            return ExceptionCodes.SMS_PHONE_ERROR;
        } else if (MontnetStatus.CONTENT_OVER_LENGTH.code == statusCode) {
            // 请求超过平台限制
            return ExceptionCodes.SMS_CONTENT_OVERLENGTH;
        }

        return null;
    }

}
